<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<title>登录</title>
	<link rel="stylesheet" type="text/css" href="css/index.css" />
	<script src="../jquery/jquery-3.6.0.js"></script>
	<link href="../layui/css/layui.css" rel="stylesheet">
</head>

<body>

<img class="bgone" src="img/1.jpg" />
<img class="pic" src="img/a.png" />

<div class="table">
	<div class="wel">神码医院后台管理系统登录</div>
	<div class="wel1">AMAZINGCODE HOSPITAL MANAGER SYSTEM LOGIN</div>
	<div class="user">
		<div id="yonghu" style=""><img src="img/yhm.png" /></div>
		<input type="text" id="username" placeholder="用户名" lay-verify="required"/>
	</div>			
	<div class="password">
		<div id="yonghu"><img src="img/mm.png" /></div>
		<input type="password" id="password" placeholder="●●●●●●" lay-verify="required"/>
	</div>
	<input class="btn" id="login" type="button" name="登录" value="登录" style="cursor: pointer"/>
</div>

</body>

<script src="../layui/layui.js"></script>
<script>
	let layer = layui.layer;
	let userid;
	$(document).ready(function() {
		// 鼠标单击事件
		$("#login").click(function () {
			let username = $("#username").val().trim();
			let password = $("#password").val().trim();
			
			if (!username) {
				layer.msg('用户名不能为空！', {icon: 7});
				return;
			}
			if (!password) {
				layer.msg('密码不能为空！', {icon: 7});
				return;
			}

			$.ajax({
				url: "/user/login",           // 请求路径
				type: "post",                  // 请求的方式，不区分大小写
				cache: false,                  // 关闭缓存
				contentType: "application/json;charset=utf-8",  // 请求的内容类型
				data: JSON.stringify({
					username: username,
					password: password
				}),
				dataType: "json",        // 返回的数据类型
				success: function(response) {
					console.log("返回: " + response);
					if (response.code === 200) {
						layer.msg('登录成功！', {icon: 6});
						localStorage.setItem("USERNAME", response.username);
						localStorage.setItem("USERID", response.id);
						window.location.href = "../patient.html";  // 跳转到首页
					} else {
						layer.msg(response.msg, {icon: 7});
					}
				},
				error: function(response) {
					alert("出错返回: " + response);
				}
			});
		});

		// 监听用户名和密码输入框的Enter键
		$("#username, #password").keydown(function(event) {
			if (event.key === "Enter") {
				$("#login").click();  // 模拟点击事件
			}
		});
	});



</script>
</html>